package com.ws.demo1;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

public class demo1 {
    public static void main(String[] args) {
        //1.JSON提供的静态方法用于，用于将Json与Java对象的交换
        //创建对象
        Student student = new Student (1,"ws",14,"女");
        //将对象转换为JSON 字符串 tojsonstring
        String jsonString = JSON.toJSONString (student);
        System.out.println ("student json =" +jsonString);
        //将集合转换为JSON字符串
        List<Student> students = new ArrayList<> ();
        students.add (new Student (1,"ws",14,"男"));
        students.add (new Student (1,"ws",14,"男"));
        students.add (new Student (1,"ws",14,"男"));
        students.add (new Student (1,"ws",14,"男"));
        String jsonString1 = JSON.toJSONString (students);
        System.out.println (jsonString1);
        //JSON字符串转换成java对象parseObject
        Student student1 = JSON.parseObject (jsonString, Student.class);
        System.out.println (student1);
        //将JSON字符串输出为集合
        List<Student> students1 = JSON.parseArray (jsonString1, Student.class);
        System.out.println (students1);
    }
}
